﻿using System;
using System.Windows;
using System.Windows.Markup;
using System.Windows.Media;

namespace personalplaner.common.controls
{
	/// 
	/// this class was created by: Dwayne Need 
	/// Description:
	/// http://blogs.msdn.com/b/dwayneneed/archive/2007/04/26/multithreaded-ui-hostvisual.aspx
	/// 
	/// Download:
	/// http://microsoftdwayneneed.codeplex.com/SourceControl/list/changesets
	/// 
	/// For more information look at 
	/// personalplaner.common.controls.VisualWraper.cs
	/// personalplaner.common.threading.VisualTargetPresentationSource.cs
	/// 
	/// personalplaner.common.controls.VisualWraper readme.txt
	/// 
	
	/// <summary>
	///     The VisualWrapper simply integrates a raw Visual child into a tree
	///     of FrameworkElements.
	/// </summary>
	public class VisualWrapper : VisualWrapper<Visual>
	{
	}

	/// <summary>
	///     The VisualWrapper simply integrates a raw Visual child into a tree
	///     of FrameworkElements.
	/// </summary>
	[ContentProperty("Child")]
	public class VisualWrapper<T> : FrameworkElement where T : Visual
	{
		public T Child
		{
			get
			{
				return _child;
			}

			set
			{
				if (_child != null)
				{
					RemoveVisualChild(_child);
				}

				_child = value;

				if (_child != null)
				{
					AddVisualChild(_child);
				}
			}
		}

		protected override Visual GetVisualChild(int index)
		{
			if (_child != null && index == 0)
			{
				return _child;
			}
			else
			{
				throw new ArgumentOutOfRangeException("index");
			}
		}

		protected override int VisualChildrenCount
		{
			get
			{
				return _child != null ? 1 : 0;
			}
		}

		private T _child;


        protected override Size ArrangeOverride(Size arrangeBounds)
        {
            return arrangeBounds;
        }

        protected override Size MeasureOverride(Size constraint)
        {
            // take all the space that is given by the parent
            return new Size(0, 0);
        }
	}
}
